package com.woniu.mealsService.service.impl;

import cn.hutool.json.JSONUtil;
import com.woniu.exception.meals.CommentException;
import com.woniu.mealsService.model.CommentFileUpload;
import com.woniu.mealsService.mapper.CommentFileUploadMapper;
import com.woniu.mealsService.service.CommentFileUploadService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.server.RedisKey;
import com.woniu.server.RedisUtil;
import com.woniu.utils.AliyunOSSUtil;
import com.woniu.utils.EnumException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 评论文件上传表 服务实现类
 * </p>
 *
 * @author jiang
 * @since 2024年12月09日
 */
@Service
public class CommentFileUploadServiceImpl extends ServiceImpl<CommentFileUploadMapper, CommentFileUpload> implements CommentFileUploadService {
    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private CommentFileUploadMapper commentFileUploadMapper;
    @Override
    public List<String> addFile(MultipartFile[] multipartFiles,long userId) throws Exception {
        if (multipartFiles.length == 0){
            throw new CommentException(EnumException.COMMENTUPLOADIMG_NULL_EXCEPTION);
        }
        // 批量上传文件，并将文件路径放入redis中
        List<String> filePathes = new ArrayList<>();
        for (MultipartFile multipartFile : multipartFiles) {
            byte[] bytes = new byte[multipartFile.getInputStream().available()];
            multipartFile.getInputStream().read(bytes);
            if (!AliyunOSSUtil.isJpg(bytes) && !AliyunOSSUtil.isPng(bytes)){
              throw new CommentException(EnumException.COMMENTUPLOADIMG_TYPE_EXCEPTION);
            }
            String suffix = StringUtils.getFilenameExtension(multipartFile.getOriginalFilename());
            String prefix = UUID.randomUUID().toString().replaceAll("-", "");
            String newFilename = prefix + "." + suffix;
            String url = aliyunOSSUtil.uploadFile(multipartFile.getInputStream(), newFilename, "jiang115");
            filePathes.add(url);
        }
        //将上传照片的数组转换为String
        String jsonStr = JSONUtil.toJsonStr(filePathes);
        //存入redis中
        redisUtil.set(RedisKey.saveUploadCommentPic(userId),jsonStr,60*60);
        return filePathes;
    }
    //批量添加文件上传数据
    @Override
    public boolean insertFilePath(List<String> filePaths, String orderId) {
        if (null == filePaths || filePaths.isEmpty()){
            return false;
        }
        List<CommentFileUpload> commentFileUploads = new ArrayList<>();
        for (String filePath : filePaths) {
            CommentFileUpload commentFileUpload = new CommentFileUpload();
            commentFileUpload.setFilePath(filePath);
            commentFileUpload.setFileName(StringUtils.getFilename(filePath));
            commentFileUpload.setFileSuffix(StringUtils.getFilenameExtension(filePath));
            commentFileUpload.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            commentFileUpload.setOrderId(Long.parseLong(orderId));
            commentFileUploads.add(commentFileUpload);
        }
        Integer count = commentFileUploadMapper.saveBash(commentFileUploads);
        if (count != filePaths.size()){
            return false;
        }
        return true;
    }

    @Override
    public boolean removeFile(List<String> filePaths) {
        if (null == filePaths || filePaths.isEmpty()){
            return false;
        }
        boolean b = false;
        for (String filePath : filePaths) {
             b = aliyunOSSUtil.deleteFile("jiang115", StringUtils.getFilename(filePath));
        }
        return b;
    }

    @Override
    public List<CommentFileUpload> getList(String orderId) {
        List<CommentFileUpload> commentFileUploads = commentFileUploadMapper.getList(orderId);
        return Collections.emptyList();
    }

    @Override
    public boolean removeList(List<Integer> commentFileUploadIds) {
        Integer count =commentFileUploadMapper.removeList(commentFileUploadIds);
        if (count == commentFileUploadIds.size()){
            return true;
        }
        return false;
    }

}
